<?php
namespace Home\Controller;
use Think\Controller;
use Think\Hook;
class IndexController extends Controller {
    public $uc;
	public $smarty;

	function _initialize(){
		Hook::add('checkHtml','Addons\Controller\Uc\UcAddon');
		Hook::add('checkAjax','Addons\Controller\Uc\UcAddon');
		Vendor('Discuz.Client');
		$this->uc = new \Client();
		$env = array(
			'controller'	=> CONTROLLER_NAME,
			'action'       => ACTION_NAME,
			'module'	    => MODULE_NAME,
			'group'		    => GROUP_NAME,
			'theme'        => THEME_NAME,
		);
		$this->assign('env', $env);
	}

	public function index(){
		$this->display();
	}

	public function logout(){
		if(IS_AJAX){
			header('Content-Type: application/json; charset=utf-8');
			echo $this->uc->logout();
		}else{
			echo $this->uc->logout();
			redirect('Index/index', 5, UCMSG_LOGOUT_SUCCESS);
		}
	}

	public function login(){
		if(IS_POST){
			if(IS_AJAX){
				header('Content-Type: application/json; charset=utf-8');
				echo $this->uc->login($_POST['username'], $_POST['password']);
			}else{
				echo $this->uc->login($_POST['username'], $_POST['password']);
				redirect('Index/index', 5, UCMSG_LOGIN_SUCCESS);
			}
		}
		$this->display('Index/index');
	}

	public function check(){
		return $this->uc->check_login();
	}

	public function getAuthcode(){
		header('Content-Type: application/json; charset=utf-8');
		if(IS_AJAX)
			if($this->check()) echo json_encode(session('auth'));
	}

	public function register(){
		if(IS_POST){
			echo $this->uc->register($_POST['username'], $_POST['password'], $_POST['email']);
			redirect('Index/index', 5, UCMSG_LOGIN_SUCCESS);
		}
		$this->display('Index/index');
	}

	public function pswd(){
		$auth = session('auth');
		if(IS_POST){
			$this->uc->uc_useredit($auth['username'], $_POST['oldpw'], $_POST['password'], $_POST['email']);
		}
		$this->assign('email', $auth['email']);
		$this->display('Index/index');
	}

	public function avatar(){
		$auth = session('auth');
		$this->assign('avatar', $this->uc->avatar($auth['uid']));
		$this->display('Index/index');
	}
}